<script setup>
import { onMounted, onUnmounted, ref,onBeforeMount, getCurrentInstance, nextTick } from "vue";
import { useRouter } from "vue-router";
import chinaMap from "@/utils/china.json";
import { getEventsDetail,getShopData } from '@/api/use';
import gameMess from '../component/gameMess.vue';
import { formatTime } from '@/utils/common'
import { getToken, removeToken } from '@/utils/auth' // 假设存在token管理工具
const router = useRouter();
// 获取全局 ECharts 实例
const { appContext } = getCurrentInstance();
const $echarts = appContext.config.globalProperties.$echarts;


// 注册地图
const tabPage = () => {
  router.push("./second");
};
const chartMap = ref(null); //地图
let chartInstance = null; 

const option = {
  animation: true,
  tooltip: {
    trigger: "item",
  },
  geo: {
    // 替换原有的bmap配置
    map: "china",
    roam: true,
    zoom: 0.8,
    scaleLimit: {
      min: 0.8, // 最小缩放比例
      max: 5, // 最大缩放比例
    },
    center: [105.07, 35.63], // 地图中心坐标（中国大致中心）
    label: {
      show: true,
      color: "#333",
    },
    itemStyle: {
      areaColor: "#f0f8ff", // 浅蓝色背景
      borderColor: "#00868B", // 青色边框
    },
    emphasis: {
      itemStyle: {
        areaColor: "#e6f7ff", // 高亮时浅蓝色
      },
    },
  },
};

const detailsData = ref({}); //活动详情
const currentDate = ref(''); //当前时间
const badgesData = ref('');
let timer = null; //展示当前时间的倒计时

// 格式化时间函数
const getTime = () => {
  currentDate.value =  formatTime();
};
//获取赛事详情
const EventsDetail = async ()=>{
  const param = {
    eventId: 60
  }
  const res = await getEventsDetail(param);
  if(res.code === 200){
    detailsData.value = res.data;
    let str = '';
    if(res.data.badges){
      res.data.badges.forEach((item,index)=>{
        if(index == res.data.badges.length - 1){
          str = str + item.typeName + item.title;
        }else{
          str = str + item.typeName + item.title +',';
        }
      })
      badgesData.value = str;
    }

  }
}
//获取商品图片
const shopList = ref([]);
const shopData = async () => {
  const params = {
    pageSize: 30,
    pageNum: 1,
  };
  const res = await getShopData(params);
  if (res.code == 200) {
    shopList.value = res.data.list;
  }
};

onBeforeMount(()=>{
  EventsDetail();
  shopData();
})

onMounted(() => {
  const token = localStorage.getItem("token");
  if (!token) {
    router.push("/");
  }
  getTime();
  timer = setInterval(getTime, 1000);
  if (chartMap.value) {
    $echarts.registerMap("china", chinaMap);
    chartInstance = $echarts.init(chartMap.value);
    chartInstance.setOption(option);
  }
});

onUnmounted(()=>{
  clearInterval(timer);
})
</script>

<template>
  <div class="main">
    <div class="mainTop" @click="tabPage">
      <img src="@/assets/img_text.png" />
      <div class="leftTop">
        <div class="leftTopAddress">
          <img src="@/assets/icon_location.png" />
          上海
        </div>
        <div class="leftTopCenter"></div>
        <div class="leftTopText">{{ currentDate }}</div>
      </div>
    </div>
    <div class="mainCenter">
      <div class="mainCenterLeft">
        <div class="top">
          <p>赛事介绍</p>
        </div>
        <div class="center">
          <div><p>赛事名称：</p>{{ detailsData.eventName }}</div>
          <div><p>赛事类型：</p>{{ detailsData.competitionTypeName }}</div>
          <div><p>赛事地点：</p>{{ detailsData.address }}</div>
          <div><p>赛事时间：</p>{{detailsData.competitionStartTime}}至{{ detailsData.competitionEndTime   }}</div>
          <div><p>赛事奖金：</p>{{ badgesData }}</div>
        </div>
        <div class="bottom">
          <div class="item">
            <div class="num">{{ detailsData.num || 0}}</div>
            <div class="text">参赛人数数量</div>
          </div>
          <div>
            <div class="item">
              <div class="num">1</div>
              <div class="text">赛道数量</div>
            </div>
          </div>
          <div>
            <div class="item">
              <div class="num">{{ detailsData.rate || '100%'}}</div>
              <div class="text">参赛率</div>
            </div>
          </div>
        </div>
      </div>
      <div class="mainCenterCenter">
        <div class="top">
          <p>赛点介绍</p>
        </div>
        <div class="mapContent" ref="chartMap"></div>
      </div>
      <div class="mainCenterRight">
        <video src="@/assets/video.mp4" autoplay muted controls loop/>
      </div>
    </div>
    <div class="mainBottom">
      <div class="mainBottomLeft">
        <div class="top">
          <p>游艇模型</p>
        </div>
        <div class="center">
          <img class="centerLeftImg" src="@/assets/img_shodow1.png" />
          <div class="centerContent">
            <div class="item" v-for="item in shopList" :key="item.id">
              <img :src="item.pictureUrl" />
            </div>
          </div>
          <img class="centerRightImg" src="@/assets/img_shodow2.png" />
        </div>
      </div> 
      <div class="mainBottomRight">
        <div class="top">
          <p>明星选手</p>
        </div>
        <div class="center">
          <div class="item">
            <img src="@/assets/head.jpg" />
            <div class="showName">彼得·莫林</div>
          </div>
          <div class="item">
            <img src="@/assets/head1.png" />
            <div class="showName">布伦特·迪拉德</div>
          </div>
          <div class="item">
            <img src="@/assets/head.jpg" />
            <div class="showName">熊梓维</div>
          </div>
          <div class="item">
            <img src="@/assets/head1.png" />
            <div class="showName">熊梓维</div>
          </div>
        </div>
      </div>
    </div>
    <gameMess></gameMess>
  </div>
</template>

<style scoped lang="scss">
.main {
  min-height: 100vh;
  padding: 20px 24px;
  background: url("@/assets/bg.png");
  background-size: 100% 100%;
  background-repeat: no-repeat;
  .mainTop {
    width: 100%;
    height: 97px;
    display: block;
    padding-top: 14px;
    position: relative;
    background: url("@/assets/top.png");
    background-size: 100% 100%;
    background-repeat: no-repeat;
    background-position: center;
    img {
      width: 504px;
      height: 53px;
    }
    .leftTop {
      top: 14px;
      left: 0;
      position: absolute;
      display: flex;
      align-items: center;
      font-size: 16px;
      font-family: AlibabaPuHuiTi_2_55_Regular;
      color: #00fffb;
      line-height: 22px;
      .leftTopAddress {
        display: flex;
        align-items: center;
        img {
          width: 32px;
          height: 30px;
          margin-right: 6px;
        }
      }
      .leftTopCenter {
        width: 2px;
        height: 24px;
        margin: 0 12px;
        background: linear-gradient(
          180deg,
          rgba(0, 177, 194, 0) 0%,
          #00b7c7 55%,
          rgba(0, 183, 199, 0) 100%
        );
      }
      .leftTopText {
        width: 159px;
        text-align: left;
        font-style: normal;
      }
    }
  }
  .mainCenter {
    margin-top: 15px;
    display: flex;
    width: 100%;
    justify-content: space-between;
    .mainCenterLeft {
      height: 468px;
      width: 412px;
      padding: 19px;
      background: url("@/assets/img3.png");
      background-size: contain;
      background-repeat: no-repeat;
      background-position: center;
      .top {
        width: 100%;
        height: 38px;
        line-height: 38px;
        font-size: 20px;
        color: #00fffb;
        text-align: left;
        background: url("@/assets/img1.png");
        background-size: contain;
        background-repeat: no-repeat;
        background-position: center;
        p {
          padding-left: 20px;
        }
      }
      .center {
        font-size: 18px;
        color: #00f4f2;
        line-height: 28px;
        text-align: left;
        div {
          margin-top: 23px;
          display: flex;
          p{
            width: 115px;
          }
        }
      }
      .bottom {
        display: flex;
        margin-top: 36px;
        justify-content: space-between;
        .item {
          width: 120px;
          height: 80px;
          .num {
            font-weight: bold;
            font-size: 36px;
            color: #00f4f2;
            line-height: 56px;
            text-align: center;
            background: url("@/assets/img4.png");
            background-size: contain;
            background-repeat: no-repeat;
            background-position: center;
          }
          .text {
            font-size: 16px;
            color: #00f4f2;
            line-height: 22px;
            margin-top: 12px;
          }
        }
      }
    }
    .mainCenterCenter {
      height: 506px;
      width: 450px;
      background: url("@/assets/img3.png");
      background-size: contain;
      background-repeat: no-repeat;
      background-position: center;
      .top {
        width: calc(100% - 38px);
        height: 38px;
        margin: 19px 19px 0px;
        line-height: 38px;
        font-size: 20px;
        color: #00fffb;
        text-align: left;
        background: url("@/assets/img1.png");
        background-size: contain;
        background-repeat: no-repeat;
        background-position: center;
        p {
          padding-left: 20px;
        }
      }
      .mapContent {
        width: 100%;
        height: 440px;
      }
      div[ref="chartMap"] {
        width: 100%;
        height: 440px;
      }
    }
    .mainCenterRight {
      width: 902px;
      height: 508px;
      
      video {
        width: 100%;
        height: 100%;
        object-fit: fill;
        border-radius: 8px;
      }
    }
  }
  .mainBottom {
    width:  100%;
    display: flex;
    margin-top: 19px;
    justify-content: space-between;
    .mainBottomLeft,
    .mainBottomRight {
      height: 386px;
      width: 926px;
      padding: 19px 0px;
      background: url("@/assets/img2.png");
      background-size: contain;
      background-repeat: no-repeat;
      background-position: center;
      .top {
        width: 412px;
        height: 38px;
        line-height: 38px;
        margin-left: 19px;
        font-size: 20px;
        color: #00fffb;
        text-align: left;
        background: url("@/assets/img1.png");
        background-size: contain;
        background-repeat: no-repeat;
        background-position: center;
        p {
          padding-left: 20px;
        }
      }
    }
    .mainBottomLeft {
      .center {
        margin: 30px 0px;
        position: relative;
        .centerLeftImg {
          left: 6px;
          top: 0;
          width: 130px;
          height: 292px;
          position: absolute;
        }
        .centerRightImg {
          right: 6px;
          top: 0;
          width: 130px;
          height: 292px;
          position: absolute;
        }
        .centerContent {
          overflow: hidden;
          display: flex;
          flex-wrap: wrap;
          margin: 0 6px;
          .item {
            width: 106px;
            height: 41px;
            border-radius: 4px;
            margin-right: 9px;
            margin-bottom: 9px;
            img {
              width: 100%;
              height: 100%;
            }
          }
          .item:nth-child(8n) {
            margin-right: 0px;
          }
        }
      }
    }
    .mainBottomRight {
      .center {
        margin-top: 35px;
        display: flex;
        justify-content: flex-start;
        .item {
          display: flex;
          flex-direction: column;
          margin: 0 25px;
          img {
            width: 180px;
            height: 220px;
          }
          .showName {
            width: 154px;
            height: 32px;
            font-weight: bold;
            line-height: 32px;
            margin: 0 auto;
            margin-top: 20px;
            font-size: 16px;
            color: #00fffb;
            text-align: center;
            background: linear-gradient(
              270deg,
              rgba(0, 255, 251, 0) 0%,
              #00fffb 49%,
              rgba(0, 255, 251, 0) 100%
            );
          }
        }
      }
    }
  }
}
</style>
